<?php

session_start();
error_reporting(E_ALL);
ini_set("display_errors", 0);
setlocale(LC_ALL, 'tr_TR.UTF-8');
require_once ("../../libs.inc.php");
include '../menu/index.php';
include '../../portal/dao/TeamDao_Class.php';
include '../../portal/dao/CountryDao_Class.php';
$teamDao = new TeamDao_Class();
$countryDao = new CountryDao_Class();

////////////////   Default page ///////////////////
header('Content-type: text/html; charset=utf-8');
$smarty->display("menu/menu.tpl");
$countries = $countryDao->loadAll();
$smarty->assign('status_radios', array(
    'ACTIVE' => 'Aktif',
    'PASSIVE' => 'Pasif'));
$options = array();
foreach ($countries as $country) {
    $options[] = array($country->getId() => $country->getName());
}
$smarty->assign("countries", $options);
///////////////////////////////////////

if (isset($_GET['delete'])) {
    $teamDao->delete($_GET['delete']);
    header("location:" . rootdir . "/view/team/");
}

if (isset($_GET['update'])) {
    $team = $teamDao->loadById($_GET['update']);
    $date = $team->getFoundedDate();
    $date = explode("-", $date);
    $smarty->assign("team", $team);
    $smarty->assign("year", $date[0]);
    $smarty->assign("month", $date[1]);
    $smarty->assign("day", $date[2]);
    $smarty->display("team/update.tpl");
}

if (isset($_GET['new'])) {
    $smarty->display("team/new.tpl");
}

if (isset($_POST['proc'])) {
    if ($_POST['proc'] == "upsert") {
        empty($_POST['id']) ? $id = null : $id = $_POST['id'];
        $team = array(
            'id' => $id,
            'name' => $_POST['name'],
            'description' => $_POST['description'],
            'foundedDate' => $_POST['Date_Year'] . "-" . $_POST['Date_Month'] . "-" . $_POST['Date_Day'],
            'country' => array('id' => $_POST['country']),
            'shortName' => $_POST['shortName'],
            'abbreviation' => $_POST['abbreviation'],
            'status' => $_POST['status'],
        );
        $teamDao->upsert($team);
    }
    header("location:" . rootdir . "/view/team/");
}


$smarty->assign("teams", $teamDao->loadAll());
$smarty->display("team/index.tpl");
?>
